package com.lanswon.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.common.enums.AnnouncementType;
import com.lanswon.common.enums.ArticleStatus;
import com.lanswon.common.enums.ArticleType;
import com.lanswon.common.util.StringUtil;
import com.lanswon.entity.ArticleVO;
import com.lanswon.service.ArticleService;

@Controller
@RequestMapping("admin/article")
public class AdminArticleController {
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping("index")
	public String index(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) {
		
		
		modelMap.addAttribute("articleTypes", ArticleType.values());
		
		return "admin/article/index";
	}
	
	@RequestMapping("pageList")
	@ResponseBody
	public Map<String, Object> pageList(HttpServletRequest request,HttpServletResponse response,Integer page,
			Integer limit,Integer articleStatus,Integer articleType, String articleName){
		Map<String, Object> viewMap = new HashMap<>();
		try {
			if(page==null) {
				page = 1;
			}
			if(limit==null) {
				limit = 10;
			}
			Map<String, Object> accessMap = new HashMap<>();
			accessMap.put("currentResult", (page-1)*limit);
			accessMap.put("endResult", limit);
			accessMap.put("articleStatus", articleStatus);
			accessMap.put("articleType", articleType);
			accessMap.put("articleName", articleName);
			accessMap.put("maxType", 2);
			Integer count = articleService.pageListCount(accessMap);
			List<ArticleVO> pageList = articleService.pageList(accessMap);
			viewMap.put("totalPages", count!=null?count%limit>0?(count/limit)+1:count/limit:0);
			viewMap.put("data", pageList);
			viewMap.put("code", 0);
			viewMap.put("count", count);  
			viewMap.put("msg", "查询成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("msg", "查询失败");
		}
		return viewMap;
	}
	
	
	@RequestMapping("articleDetail")
	public String articleDetail(HttpServletRequest request,HttpServletResponse response,Long articleId,ModelMap modelMap){
		try {
			ArticleVO articleVO = null;
			if(articleId != null) {
				articleVO = articleService.findArticleById(articleId);
			}
			modelMap.addAttribute("article", articleVO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "admin/article/articleDetail";
	}
	
	
	@RequestMapping("articleDel")
	@ResponseBody
	public Map<String, Object> articleDel(HttpServletRequest request,HttpServletResponse response,Long articleId){
		Map<String, Object> viewMap = new HashMap<>();
		try {
			if(articleId!=null) {
				articleService.deleteArticle(articleId);
			}
			viewMap.put("code", 1);
			viewMap.put("msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 1);
			viewMap.put("msg", "操作失败");
		}
		return viewMap;
	}
	
	
	@RequestMapping("articleUpdate")
	public String articleUpdate(HttpServletRequest request,HttpServletResponse response, Long articleId,ModelMap modelMap) {
		ArticleVO articleVO = null;
		if(articleId != null) {
			articleVO = articleService.findArticleById(articleId);
		}
		modelMap.addAttribute("articleTypes", ArticleType.values());
		modelMap.addAttribute("articleStatuses", ArticleStatus.values());
		modelMap.addAttribute("announcementTypes", AnnouncementType.values());
		modelMap.addAttribute("article", articleVO);
		return "admin/article/articleUpdate";
	}
	
	@RequestMapping("updateArticle")
	@ResponseBody
	public Map<String, Object> updateArticle(HttpServletRequest request,HttpServletResponse response, ArticleVO articleVO){
		Map<String, Object> viewMap = new HashMap<>();
		try {
			System.out.println(articleVO.getReleaseTimeStr());
			if(articleVO.getReleaseTimeStr()!=null) {
				articleVO.setReleaseTime(StringUtil.strToDate(articleVO.getReleaseTimeStr(), "yyyy-MM-dd"));
			}
			if(articleVO.getAnnouncementType()==null){
				articleVO.setAnnouncementType(AnnouncementType.ANNOUNCEMENT_OTHER.getCode());
			}
			if(articleVO.getArticleId()!=null) {
				articleService.updateArticle(articleVO);
			}else {
				articleService.addArticle(articleVO);
			}
			viewMap.put("code", 1);
			viewMap.put("msg", "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("msg", "保存失败");
		}
		return viewMap;
	}
	
}
